const arrayProto = Array.prototype
export const arrayMethods = Object.create(arrayProto)

const methodsToPatch = [
  'push',
  'pop',
  'shift',
  'unshift',
  'sort',
  'splice',
  'reverse'
]

methodsToPatch.forEach(function (method) {
  arrayMethods[method] = function (...args) {
    console.log('用户调用了' + method + '方法');
    const result = arrayProto[method].apply(this, args)
    let inserted
    let ob = this.__ob__

    switch (method) {
      case 'push':
      case 'unshift':
        inserted = args
        break
      case 'splice':
        inserted = args.slice(2)
        break
      default:
        break
    }
    if (inserted) {
      ob.observeArray(inserted)
    }
    return result
  }
})
